//
//  MLTabBar.swift
//  MLWeiBo
//
//  Created by wenxing on 2017/1/4.
//  Copyright © 2017年 Se7venX. All rights reserved.
//

import UIKit

class MLTabBar: UITabBar {

    //MARK:- 属性
    var composeClosure:(()->())?
    
  
    //MARK:- 初始化方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(compostButton)
        compostButton.addTarget(self, action: #selector(clickComposeButton), for: .touchUpInside)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK:- Private
    @objc private func clickComposeButton() {
    
        composeClosure?()
    
    }
    
    //MARK:- layout
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let w = UIScreen.main.bounds.width / 5
        let h = self.bounds.height
        
        var index = 0
        
        for view in subviews {
            
            if view.isKind(of: NSClassFromString("UITabBarButton")!) {
             
                // 修改frame
                view.frame = CGRect(x: CGFloat(index) * w, y: 0, width: w, height: h)
                
                index += (index==1 ? 2 : 1)
            }
            
        }
        
        //设置compose的按钮frame
        compostButton.bounds = CGRect(x: 0, y: 0, width: w, height: h)
        compostButton.center = CGPoint(x: self.bounds.width*0.5, y: h*0.5)
        
    }
    
    
    lazy var compostButton: UIButton = {
        
        let btn = UIButton()
        
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add"), for: .normal)
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button"), for: .normal)
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button_highlighted"), for: .highlighted)

        return btn
    }()

}
